// 时间格式转换 返回 (年-月-日 时:分:秒)
function timeFormat(time) {
	let date = new Date(time);
	let year = date.getFullYear();
	let month = date.getMonth();
	let day = date.getDate();
	let h = date.getHours();
	let m = date.getMinutes();
	let s = date.getSeconds()
	return year + '-' + haveZero(month) + '-' + haveZero(day) + ' ' + haveZero(h) + ':' + haveZero(m) + ':' + haveZero(s);
}

// 时间格式转换 返回 (年-月-日/时:分)
function timeSf(time) {
	let date = new Date(time);
	let year = date.getFullYear();
	let month = date.getMonth();
	let day = date.getDate();
	let h = date.getHours();
	let m = date.getMinutes();
	return year + '-' + haveZero(month+1) + '-' + haveZero(day) + '/' + haveZero(h) + ':' + haveZero(m);
}

// 时间格式转换 返回 (年/月/日/时:分)
function timex(time) {
	let date = new Date(time);
	let year = date.getFullYear();
	let month = date.getMonth();
	let day = date.getDate();
	let h = date.getHours();
	let m = date.getMinutes();
	return year + '/' + haveZero(month) + '/' + haveZero(day) + '/' + haveZero(h) + ':' + haveZero(m);
}

// 返回年-月-日
function getDate(data, splitor = '-') {
	let date = new Date(data);
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return `${year}${splitor}${haveZero(month)}${splitor}${haveZero(day)}`
}

// 今天 --时/分
// 昨天 --时/分
// 前天 --时/分
// 年-月-日 时分
function getTime(time) {
	let date = new Date(time);
	const year = date.getFullYear();
	const month = date.getMonth();
	const hour = date.getHours()
	const minute = date.getMinutes()
	var today = new Date().getDate();
	var day = date.getDate();
	var result, offset;
	var days = parseInt((new Date().getTime() - date) / 86400000);
	offset = Math.abs(today - day);
	if (days < 3 && offset < 3) {
		if (offset === 0) {
			result = hour + ":" + haveZero(minute);
		} else if (offset === 1) {
			result = "昨天 " + hour + ":" + haveZero(minute);
		} else if (offset === 2) {
			result = "前天 " + hour + ":" + haveZero(minute);
		}
	} else {
		result = year + "-" + haveZero(month) + "-" + haveZero(day) + " " + haveZero(hour) + ":" + haveZero(minute);
	}
	return result
}

// 返回时分秒/时分
function transDate(time, withSecond = false) {
	let date = new Date(time);
	const year = date.getFullYear();
	const month = date.getMonth();
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return withSecond ? `${haveZero(hour)}:${haveZero(minute)}:${haveZero(second)}` : `${hour}:${haveZero(minute)}`
}

function add0(m){return m<10?'0'+m:m }

function timeFormatX(timestamp){
  //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = time.getMonth()+1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds);
}

// 时间格式转化 --- 判断数字见面是否补0
function haveZero(num) {
	return num < 10 ? '0' + num : num;
}

export default {
	timeFormat,
	timeSf,
	getDate,
	getTime,
	transDate,
	timex,
	timeFormatX
}
